package com.yesorno.model;

import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.yesorno.activity.LoginActivity;
import com.yesorno.activity.R;
import com.yesorno.constants.Constants;

public class MyTimerTask extends TimerTask {
	private static Context context;

	private static MyTimerTask myTimerTask;

	public static MyTimerTask getInstance(Context ct) {
		if (myTimerTask == null) {
			context = ct;
			myTimerTask = new MyTimerTask(context);
		}
		return myTimerTask;
	}

	public MyTimerTask(Context context) {
	}

	@Override
	public void run() {
		System.out.println("执行中^^^");
		checkMsg(context);
	}

	// 声明通知（消息）管理器
	static NotificationManager m_NotificationManager;
	static Intent m_Intent;
	static PendingIntent m_PendingIntent;
	// 声明Notification对象
	static Notification m_Notification;
	public static final int MY_NOTIFICATION_ID = 0x00999999;// 在消息栏中对应的id
	public static boolean isNotify = false;// 是否已通知在消息栏

	private static Handler mHandler = new Handler() {
		public void handleMessage(Message message) {
			Log.i("msgCheck", "message what = " + message.what);
		}
	};

	public static void checkMsg(Context context) {
		// 初始化NotificationManager对象
		m_NotificationManager = (NotificationManager) context
				.getSystemService(Activity.NOTIFICATION_SERVICE);
		// 点击通知时转移内容
		m_Intent = new Intent(context, LoginActivity.class);
		// 主要是设置点击通知时显示内容的类
		m_PendingIntent = PendingIntent.getActivity(context, 0, m_Intent, 0);
		// 构造Notification对象
		m_Notification = new Notification(R.drawable.icon, context
				.getResources().getString(R.string.task_loading),
				System.currentTimeMillis());
		// 这是设置通知是否同时播放声音或振动，声音为Notification.DEFAULT_SOUND
		// 振动为Notification.DEFAULT_VIBRATE;
		// Light为Notification.DEFAULT_LIGHTS，在我的Milestone上好像没什么反应
		// 全部为Notification.DEFAULT_ALL
		// 如果是振动或者全部，必须在AndroidManifest.xml加入振动权限
		if (Constants.TASK_ALERT_TYPE == Constants.TASK_ALERT_TYPE_VIBRATOR) {
			m_Notification.defaults = Notification.DEFAULT_VIBRATE;
		}
		if (Constants.TASK_ALERT_TYPE == Constants.TASK_ALERT_TYPE_RING) {
			m_Notification.defaults = Notification.DEFAULT_SOUND;
		}

		// 设置通知显示的参数
		m_Notification.setLatestEventInfo(context, context.getResources()
				.getString(R.string.app_name), context.getResources()
				.getString(R.string.task_alert), m_PendingIntent);
		// 可以理解为执行这个通知
		m_NotificationManager.notify(MY_NOTIFICATION_ID, m_Notification);
		Message msg = mHandler.obtainMessage();
		msg.sendToTarget();
		isNotify = true;
	}

	/**
	 * 删除自己的任务图标
	 */
	public static void removeMyTask() {
		if (isNotify) {
			m_NotificationManager.cancel(MY_NOTIFICATION_ID);
			isNotify = false;
		}
	}

}
